import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入仓库
import store from '../store'
// 封装一个函数，确定是否含有登录的返回地址
function hasUrl(url, next) {
  //  通过some 进行查找只要存在即为真
  let result = store.getters.getUser.menus_url.some((item) => item == url);
  if (result) {
    next()
  } else {
    next('/home')
  }
}

Vue.use(VueRouter)
// 设置一个路线变量
export const indexRoutes = [
  {
    path: '/menu',
    component: () => import('../views/menu/menu'),
    meta: {
      title: '菜单管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/menu", next)
    }
  },
  {
    path: '/role',
    component: () => import('../views/role/role'),
    meta: {
      title: '角色管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/role", next)
    }
  },
  {
    path: '/user',
    component: () => import('../views/user/user'),
    meta: {
      title: '管理员管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/user", next)
    }
  },
  {
    path: '/cate',
    component: () => import('../views/cate/cate'),
    meta: {
      title: '商品分类'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/cate", next)
    }

  },
  {
    path: '/specs',
    component: () => import('../views/specs/specs'),
    meta: {
      title: '商品规格'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/specs", next)
    }

  },
  {
    path: '/goods',
    component: () => import('../views/goods/goods'),
    meta: {
      title: '商品管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/goods", next)
    }

  },
  {
    path: '/member',
    component: () => import('../views/member/member'),
    meta: {
      title: '会员管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/member", next)
    }

  },
  {
    path: '/banner',
    component: () => import('../views/banner/banner'),
    meta: {
      title: '轮播图管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/banner", next)
    }

  },
  {
    path: '/seck',
    component: () => import('../views/seck/seck'),
    meta: {
      title: '限时秒杀管理'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/seck", next)
    }

  },
  {
    path: '/map',
    component: () => import('../views/map/map'),
    meta: {
      title: '地图服务'
    },
    beforeEnter: (to, from, next) => {
      hasUrl("/map", next)
    }

  },
]

const routes = [
  {
    path: "/index",
    component: () => import("../pages/index.vue"),
    children: [
      {
        path: '/home',
        component: () => import('../views/home')
      },
     ...indexRoutes,
      {
        path: '',
        redirect: '/home'
      }
    ]
  },
  {
    path: '/login',
    component: () => import("../pages/login.vue")
  },
  {
    path: "*",
    redirect: "/index"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

// 全局登录拦截
router.beforeEach((to, from, next) => {
  // 1.如果去的是登录就next
  if (to.path == '/login') {
    next()
    return
  }
  // 2.如果有登录信息就next
  if (store.getters.getUser) {
    next()
    return
  }
  // 3.以上逻辑都不符合就直接登录
  next('/login')
})

export default router
